package com.csu.mall.product.web;


import com.csu.mall.common.utils.R;
import com.csu.mall.common.vo.MemberResponseVo;
import com.csu.mall.product.entity.CategoryEntity;
import com.csu.mall.product.service.CategoryService;
import com.csu.mall.product.vo.Catelog2Vo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Resource
    private CategoryService categoryService;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model){
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categories();
        model.addAttribute("categorys", categoryEntities);
        return "index";
    }

    //index/catalog.json
    @ResponseBody
    @GetMapping("/index/json/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatalogJson(){
        Map<String, List<Catelog2Vo>> catalogJson = categoryService.getCatalogJson();
        return catalogJson;
    }

    @GetMapping("/test")
    public void test(HttpSession session){
        System.out.println(session);
        System.out.println((MemberResponseVo) session.getAttribute("sessionAttr:login_user"));
        System.out.println("session: " + session.toString());
    }
}
